/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.camachosanchez.controler.jpa;

import com.camachosanchez.bean.DetalleDespinche;
import com.camachosanchez.bean.InsumosDespinche;
import com.camachosanchez.controler.jpa.exceptions.IllegalOrphanException;
import com.camachosanchez.controler.jpa.exceptions.NonexistentEntityException;
import com.camachosanchez.util.JPAUtil;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;

/**
 *
 * @author Rolando
 */
public class InsumosDespincheJpaController implements Serializable {

    public InsumosDespincheJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return JPAUtil.getEntityManagerFactory().createEntityManager();
    }

    public void create(InsumosDespinche insumosDespinche) {
        if (insumosDespinche.getDetalleDespincheList() == null) {
            insumosDespinche.setDetalleDespincheList(new ArrayList<DetalleDespinche>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<DetalleDespinche> attachedDetalleDespincheList = new ArrayList<DetalleDespinche>();
            for (DetalleDespinche detalleDespincheListDetalleDespincheToAttach : insumosDespinche.getDetalleDespincheList()) {
                detalleDespincheListDetalleDespincheToAttach = em.getReference(detalleDespincheListDetalleDespincheToAttach.getClass(), detalleDespincheListDetalleDespincheToAttach.getId());
                attachedDetalleDespincheList.add(detalleDespincheListDetalleDespincheToAttach);
            }
            insumosDespinche.setDetalleDespincheList(attachedDetalleDespincheList);
            em.persist(insumosDespinche);
            for (DetalleDespinche detalleDespincheListDetalleDespinche : insumosDespinche.getDetalleDespincheList()) {
                InsumosDespinche oldIdInsumosDespincheOfDetalleDespincheListDetalleDespinche = detalleDespincheListDetalleDespinche.getIdInsumosDespinche();
                detalleDespincheListDetalleDespinche.setIdInsumosDespinche(insumosDespinche);
                detalleDespincheListDetalleDespinche = em.merge(detalleDespincheListDetalleDespinche);
                if (oldIdInsumosDespincheOfDetalleDespincheListDetalleDespinche != null) {
                    oldIdInsumosDespincheOfDetalleDespincheListDetalleDespinche.getDetalleDespincheList().remove(detalleDespincheListDetalleDespinche);
                    oldIdInsumosDespincheOfDetalleDespincheListDetalleDespinche = em.merge(oldIdInsumosDespincheOfDetalleDespincheListDetalleDespinche);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(InsumosDespinche insumosDespinche) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            InsumosDespinche persistentInsumosDespinche = em.find(InsumosDespinche.class, insumosDespinche.getId());
            List<DetalleDespinche> detalleDespincheListOld = persistentInsumosDespinche.getDetalleDespincheList();
            List<DetalleDespinche> detalleDespincheListNew = insumosDespinche.getDetalleDespincheList();
            List<String> illegalOrphanMessages = null;
            for (DetalleDespinche detalleDespincheListOldDetalleDespinche : detalleDespincheListOld) {
                if (!detalleDespincheListNew.contains(detalleDespincheListOldDetalleDespinche)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain DetalleDespinche " + detalleDespincheListOldDetalleDespinche + " since its idInsumosDespinche field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<DetalleDespinche> attachedDetalleDespincheListNew = new ArrayList<DetalleDespinche>();
            for (DetalleDespinche detalleDespincheListNewDetalleDespincheToAttach : detalleDespincheListNew) {
                detalleDespincheListNewDetalleDespincheToAttach = em.getReference(detalleDespincheListNewDetalleDespincheToAttach.getClass(), detalleDespincheListNewDetalleDespincheToAttach.getId());
                attachedDetalleDespincheListNew.add(detalleDespincheListNewDetalleDespincheToAttach);
            }
            detalleDespincheListNew = attachedDetalleDespincheListNew;
            insumosDespinche.setDetalleDespincheList(detalleDespincheListNew);
            insumosDespinche = em.merge(insumosDespinche);
            for (DetalleDespinche detalleDespincheListNewDetalleDespinche : detalleDespincheListNew) {
                if (!detalleDespincheListOld.contains(detalleDespincheListNewDetalleDespinche)) {
                    InsumosDespinche oldIdInsumosDespincheOfDetalleDespincheListNewDetalleDespinche = detalleDespincheListNewDetalleDespinche.getIdInsumosDespinche();
                    detalleDespincheListNewDetalleDespinche.setIdInsumosDespinche(insumosDespinche);
                    detalleDespincheListNewDetalleDespinche = em.merge(detalleDespincheListNewDetalleDespinche);
                    if (oldIdInsumosDespincheOfDetalleDespincheListNewDetalleDespinche != null && !oldIdInsumosDespincheOfDetalleDespincheListNewDetalleDespinche.equals(insumosDespinche)) {
                        oldIdInsumosDespincheOfDetalleDespincheListNewDetalleDespinche.getDetalleDespincheList().remove(detalleDespincheListNewDetalleDespinche);
                        oldIdInsumosDespincheOfDetalleDespincheListNewDetalleDespinche = em.merge(oldIdInsumosDespincheOfDetalleDespincheListNewDetalleDespinche);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Short id = insumosDespinche.getId();
                if (findInsumosDespinche(id) == null) {
                    throw new NonexistentEntityException("The insumosDespinche with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Short id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            InsumosDespinche insumosDespinche;
            try {
                insumosDespinche = em.getReference(InsumosDespinche.class, id);
                insumosDespinche.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The insumosDespinche with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<DetalleDespinche> detalleDespincheListOrphanCheck = insumosDespinche.getDetalleDespincheList();
            for (DetalleDespinche detalleDespincheListOrphanCheckDetalleDespinche : detalleDespincheListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This InsumosDespinche (" + insumosDespinche + ") cannot be destroyed since the DetalleDespinche " + detalleDespincheListOrphanCheckDetalleDespinche + " in its detalleDespincheList field has a non-nullable idInsumosDespinche field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(insumosDespinche);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<InsumosDespinche> findInsumosDespincheEntities() {
        return findInsumosDespincheEntities(true, -1, -1);
    }

    public List<InsumosDespinche> findInsumosDespincheEntities(int maxResults, int firstResult) {
        return findInsumosDespincheEntities(false, maxResults, firstResult);
    }

    private List<InsumosDespinche> findInsumosDespincheEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from InsumosDespinche as o where o.estado = 1");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public InsumosDespinche findInsumosDespinche(Short id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(InsumosDespinche.class, id);
        } finally {
            em.close();
        }
    }

    public int getInsumosDespincheCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from InsumosDespinche as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
